<template>
  <view class="container">
    <view class="grid-body mt-30" v-for="(item, index) in dataList" :key="index">
      <view class="check grid-common">
        <view class="title flex">{{ index + 1 }} {{ item.itemName }}</view>
        <view class="flex text blue-dot">排查内容</view>
        <view class="flex text">
          <text>{{ item.itemContent }}</text>
        </view>
        <view class="bor-top"></view>
        <view class="flex text blue-dot">排查依据</view>
        <view class="flex text">
          <text>{{ item.itemBasis }}</text>
        </view>
        <view class="bor-top"></view>
        <view class="flex text blue-dot">排查方法</view>
        <view class="flex text">
          <text>{{ item.itemMethod }}</text>
        </view>
        <view class="bor-top"></view>
        <view class="flex text blue-dot">排查状态</view>
        <view class="flex text" v-if="item.isDispose === '1'">
          <text>已排查</text>
        </view>
        <view class="flex text" v-else>
          <text class="color-A14430">未排查</text>
        </view>
        <view v-if="item.isDispose !== '1'" class="button-wrap">
          <button class="u-button block" @click="checkNormal(item, index)">正常</button>
          <button class="u-button u-button-danger block" @click="goReport(item, index)">上报隐患</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { getItem, completeCheck } from '@/api/dbd/hiddenDangerCheck.js'
import { managerSourceEnum } from '@/utils/constant.js'
export default {
  data() {
    return {
      detail: {},
      managerSourceEnum,
      planTaskId: null,
      subTaskId: null,
      dataList: [],
      location: '',
      riskName: ''
    }
  },
  onLoad(option) {
    this.planTaskId = option.planTaskId
    this.subTaskId = option.subTaskId
    this.location = option.location
    this.riskName = option.riskName
    this.getData()
  },
  methods: {
    getData() {
      getItem(this.subTaskId).then((res) => {
        this.detail = res.data
        this.dataList = JSON.parse(res.data.troubleshooting)
      })
    },
    checkNormal(item, index) {
      const _that = this
      this.$modal.confirm('隐患排查正常，确定执行此操作？').then(async () => {
        item['isDispose'] = '1'
        this.dataList.splice(index, 1, item)
        const troubleshootingCompleteCount = this.getCompleteCount()
        await completeCheck({
          subTaskId: this.subTaskId,
          troubleshooting: JSON.stringify(this.dataList),
          troubleshootingCompleteCount: this.getCompleteCount(),
          troubleshootingCount: this.dataList.length
        })
        if (troubleshootingCompleteCount === this.dataList.length) {
          const eventChannel = this.getOpenerEventChannel()
          eventChannel.emit('callback:reload')
          uni.navigateBack()
        }
      })
    },
    // 排查项已排查数量
    getCompleteCount() {
      let count = 0
      for (var i = 0; i < this.dataList.length; i++) {
        if (this.dataList[i].isDispose === '1') {
          count++
        }
      }
      return count
    },
    goReport(item, index) {
      let troubleshooting = JSON.stringify(item)
      const leadDeptId = this.detail.leadDeptId || ''
      const leadDeptName = this.detail.leadDeptName || ''
      uni.navigateTo({
        url:
          `/pages/work/hiddenDangerReport/index?managerSource=${managerSourceEnum.HIDDENDANGER}&subTaskId=` +
          this.subTaskId +
          '&location=' +
          this.location +
          '&riskName=' +
          this.riskName +
          '&managerSourceId=' +
          this.planTaskId +
          '&index=' +
          index +
          '&deptId=' +
          leadDeptId +
          '&deptName=' +
          leadDeptName +
          '&troubleshooting=' +
          troubleshooting,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': async (data) => {
            this.$set(this.dataList[data.index], 'isDispose', '1')
            const troubleshootingCompleteCount = this.getCompleteCount()
            await completeCheck({
              subTaskId: data.subTaskId,
              troubleshooting: JSON.stringify(this.dataList),
              troubleshootingCompleteCount: troubleshootingCompleteCount,
              troubleshootingCount: this.dataList.length
            })
            if (troubleshootingCompleteCount === this.dataList.length) {
              const eventChannel = this.getOpenerEventChannel()
              eventChannel.emit('callback:reload')
              uni.navigateBack()
            }
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding-bottom: 120px;
}
.check {
  position: relative;

  .title {
    font-size: 34rpx;
    font-weight: 700;
  }

  .icon-style {
    width: 52rpx;
    height: 52rpx;
    margin-right: 20rpx;
  }

  .text {
    color: #999;

    text {
      font-size: 28rpx;
      color: #666;
    }
  }

  .blue-dot {
    position: relative;
    padding-left: 20rpx;

    &::before {
      position: absolute;
      content: '';
      left: 0;
      top: 6rpx;
      width: 8rpx;
      height: 28rpx;
      background-color: #c4c088;
      border-radius: 6rpx;
    }
  }

  .bor-top {
    margin-top: 20rpx;
    height: 1px;
    line-height: 1px;
    border-bottom: 1rpx dashed #d5d7e3;
    border-top: none;
  }

  .check-right-top {
    position: absolute;
    right: 20rpx;
    top: 40rpx;
    image {
      width: 100rpx;
      height: 88rpx;
    }
  }
}
.submit-group {
  background: #fff;
  z-index: 9;
  padding: 30px;
}
.grid-body {
  margin-left: 0;
  margin-right: 0;
}

.button-wrap {
  padding: 30rpx 0;
  display: flex;
}
</style>
